package com.newtouch.bxzs.database.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.newtouch.bxzs.common.base.response.BaseResponse;
import com.newtouch.bxzs.common.base.response.RespGenerator;
import com.newtouch.bxzs.database.config.MyLog;
import com.newtouch.bxzs.database.model.bo.AppUserList4AddPcUserBO;
import com.newtouch.bxzs.database.model.bo.PcUseDetailBO;
import com.newtouch.bxzs.database.model.bo.PcUserListBO;
import com.newtouch.bxzs.database.model.bo.UserMenuListBO;
import com.newtouch.bxzs.database.model.vo.*;
import com.newtouch.bxzs.database.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 维护后台用户信息:
 *
 * <p>
 * <b>History:</b>
 * <table border="1">
 * <tr>
 * <th>Date</th>
 * <th>Operator</th>
 * <th>Memo</th>
 * </tr>
 * <tr>
 * <td>2021/1/6 17:30</td>
 * <td>tianshangzhu</td>
 * <td>Create</td>
 * </tr>
 * </table>
 *
 * @author tianshangzhu
 * @version 1.0.0
 * @since 1.0.0
 */
@RestController
@RequestMapping("/pcUser")
@Api(tags = "维护后台用户信息")
@SuppressWarnings("unchecked")
public class PcUserController {
    /**
     * 静态变量：系统日志
     */
    private static final Log logger = LogFactory.getLog(BaseCommonController.class);
    @Resource
    private UserService userService;

    @PostMapping("/getUserList")
    @ApiOperation("查询后台用户列表信息")
    @MyLog(systemFunctionName = "维护后台用户信息", systemFunctionUrl = "/pcUser/getUserList", userOperationType = 4, userOperationSummary = "查询用户列表")
    public BaseResponse<IPage<PcUserListBO>> getUserList4Pc(@RequestBody GetPcUserListVO getPcUserListVO) {
        logger.info("查询后台用户列表信息");
        IPage<PcUserListBO> result = userService.getUserList4Pc(getPcUserListVO);
        if (result == null || result.getTotal() == 0) {
            return RespGenerator.returnOK("没有查询到相应数据");
        }
        return RespGenerator.returnOK(result);
    }

    @GetMapping("/getUserDetail")
    @ApiOperation("查看后台用户详情信息")
    @MyLog(systemFunctionName = "维护后台用户信息", systemFunctionUrl = "/pcUser/getUserDetail", userOperationType = 4, userOperationSummary = "查看用户详情")
    public BaseResponse<PcUseDetailBO> getUserDetail4Pc(@RequestParam @ApiParam(value = "用户id", required = true) String userId) {
        logger.info("查看后台用户详情信息");
        return RespGenerator.returnOK(userService.getUserDetail4Pc(userId));
    }

    @PostMapping("/updateUser")
    @ApiOperation("添加/修改后台用户信息")
    @MyLog(systemFunctionName = "维护后台用户信息", systemFunctionUrl = "/pcUser/updateUser", userOperationType = 5, userOperationSummary = "添加/修改用户")
    public BaseResponse<String> savePcUser(@RequestBody UpdatePcUserVO updatePcUserVO) {
        logger.info("添加/修改后台用户信息");
        userService.savePcUser(updatePcUserVO);
        return RespGenerator.returnOK("操作成功");
    }

    @PostMapping("/deleteUser")
    @ApiOperation("删除后台用户信息")
    @MyLog(systemFunctionName = "维护后台用户信息", systemFunctionUrl = "/pcUser/deleteUser", userOperationType = 3, userOperationSummary = "删除用户")
    public BaseResponse<String> deletePcUser(@RequestBody @ApiParam(value = "pc用户id,传数组，不用带参数名", required = true) List<String> userIdList) {
        logger.info("删除后台用户信息");
        userService.deletePcUserByUserId(userIdList);
        return RespGenerator.returnOK("删除成功");
    }

    @GetMapping("/resetPassword")
    @ApiOperation("重置后台用户登录密码")
    @MyLog(systemFunctionName = "维护后台用户信息", systemFunctionUrl = "/pcUser/resetPassword", userOperationType = 2, userOperationSummary = "重置用户密码")
    public BaseResponse<String> resetPassword(@RequestParam @ApiParam(value = "pc用户id", required = true) String userId) {
        logger.info("重置后台用户登录密码");
        userService.resetPassword4Pc(userId);
        return RespGenerator.returnOK("重置用户密码成功");
    }

    @PostMapping("/getAppUserList")
    @ApiOperation("后台用户添加/用户选择")
    @MyLog(systemFunctionName = "维护后台用户信息", systemFunctionUrl = "/pcUser/getAppUserList",
            userOperationType = 4, userOperationSummary = "查询App用户列表")
    public BaseResponse<IPage<AppUserList4AddPcUserBO>> getAppUserList(@RequestBody GetAppUserListVO getAppUserListVO) {
        logger.info("后台用户添加/用户选择");
        IPage<AppUserList4AddPcUserBO> result = userService.getAppUserList4AddPcUser(getAppUserListVO);
        if (result == null || result.getTotal() == 0) {
            return RespGenerator.returnOK("没有查询到相应数据");
        }
        return RespGenerator.returnOK(result);
    }

    @PostMapping("/getMenuList")
    @ApiOperation("获取用户的所有菜单列表")
    @MyLog(systemFunctionName = "维护后台用户信息", systemFunctionUrl = "/pcUser/getMenuList",
            userOperationType = 4, userOperationSummary = "获取用户所有菜单")
    public BaseResponse<UserMenuListBO> getUserMenuList(@RequestBody GetUserMenuListVO getUserMenuListVO) {
        logger.info("获取用户的所有菜单列表");
        UserMenuListBO userMenuList = userService.getUserMenuList(getUserMenuListVO);
        if (BeanUtil.isEmpty(userMenuList)) {
            return RespGenerator.returnOK("没有查询到相应数据");
        }
        return RespGenerator.returnOK(userMenuList);
    }

    @PostMapping("/updateState")
    @ApiOperation("启用/禁用后台用户")
    @MyLog(systemFunctionName = "维护后台用户信息", systemFunctionUrl = "/pcUser/updateState",
            userOperationType = 2, userOperationSummary = "启用/禁用用户")
    public BaseResponse<Object> updateState(@RequestBody UpdateStateVO updateStateVO) {
        logger.info("启用/禁用后台用户");
        userService.updatePcUserState(updateStateVO);
        return RespGenerator.returnOK("操作成功");
    }
}
